#!bin/bash
echo -e "\033[32m zabbix_server IP: \033[0m"
read S_ip
echo -e "\033[32m zabbix_agent IP: \033[0m"
read A_ip

[ -w /etc/group ] && Permission="yes"||Permission="no" && echo $Permission
if [ "$Permission" = "no" ]; then
 chattr -i /etc/passwd && chattr -i /etc/group && chattr -i /etc/gshadow && chattr -i /etc/shadow
 groupadd zabbix && useradd -g zabbix zabbix
 chattr +i /etc/passwd && chattr +i /etc/group && chattr +i /etc/gshadow && chattr +i /etc/shadow
 else
 groupadd zabbix && useradd -g zabbix zabbix
fi

yum install wget vim tree gcc gcc-c++ pcre* -y
cd /opt && wget https://mirror.cnop.net/zabbix/zabbix-6.0.1.tar.gz
tar zxvf zabbix-6.0.1.tar.gz && cd zabbix-6.0.1
./configure  --prefix=/usr/local/zabbix --enable-agent
make && make install

mkdir /var/log/zabbix &&  chown zabbix.zabbix /var/log/zabbix
cp /opt/zabbix-6.0.1/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
chmod 755 /etc/init.d/zabbix_agentd

echo -e "\033[32m configure: \033[0m"
sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g" /etc/init.d/zabbix_agentd
ln -s /usr/local/zabbix/etc/ /etc/zabbix
ln -s /usr/local/zabbix/bin/zabbix_zabbix_get /usr/bin/zabbix_zabbix_get
ln -s /usr/local/zabbix/bin/zabbix_sender /usr/bin/zabbix_sender
ln -s /usr/local/zabbix/sbin/*  /usr/sbin/

sed -i "s@Server=127.0.0.1@Server=$S_ip@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@ServerActive=127.0.0.1@ServerActive=$S_ip:10051@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@tmp/zabbix_agentd.log@var/log/zabbix/zabbix_agentd.log@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@^# UnsafeUserParameters=0@UnsafeUserParameters=1\n@g" /etc/zabbix/zabbix_agentd.conf

sed -i "s@Hostname=Zabbix server@Hostname=$A_ip@g" /etc/zabbix/zabbix_agentd.conf
rm -rf /opt/zabbix-6.0.1*
echo -e "\033[32m Done. \033[0m"
sleep 2
echo -e "\033[32m Start zabbix_agentd: \033[0m"
chkconfig zabbix_agentd on
service zabbix_agentd start
netstat -lnet |grep 10050